package com.gscitysfy.cus.drain.modules.common.controller;

import com.gscitysfy.cloudfrmwk.assistant.domain.Result;
import com.gscitysfy.cus.drain.modules.common.entity.DrainInspectionMnFile;
import com.gscitysfy.cus.drain.modules.common.service.IFileService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @author 葛守跃
 * @version 1.0.0
 * @ClassName MinioFileController
 * @Description 云文件相关操作
 * @creatTime 2022/3/24 15:19
 */
@RestController
@RequestMapping("/common")
@Slf4j
@Tag(name = "通用接口", description = "云文件相关操作")
public class MinioFileController {

    @Resource
    private IFileService fileService;

    /**
     * 文件上传
     * @return
     * @throws IOException
     */
    @PostMapping("/uploadFile")
    @Operation(summary = "附件上传，将文件上传至minio服务器，并返回上传信息")
    public Result<DrainInspectionMnFile> uploadFile(MultipartFile file) throws IOException {
        DrainInspectionMnFile commonFile = fileService.uploadFile(file);
        return Result.content(commonFile);
    }

    /**
     * 根据文件id获取文件信息
     * @return
     */
    @GetMapping("/getFileInfo")
    @Operation(summary = "根据文件id获取附件信息")
    public Result<DrainInspectionMnFile> getFileInfo(String id){
        return Result.content(fileService.getById(id));
    }

    /**
     * 根据文件id下载文件
     * @param id
     * @return
     */
    @GetMapping("/downloadFileByFileId")
    @Operation(summary = "根基文件id获取附件信息")
    public ResponseEntity<byte[]> downloadFileByFileId(Long id) throws IOException {
        return fileService.downloadFileByFileId(id);
    }




}
